//
//  UIButton+Add.m
//  OCTools
//
//  Created by 王俊杰 on 2019/10/31.
//  Copyright © 2019 赵卫军. All rights reserved.
//

#import "UIButton+Add.h"

@implementation UIButton (Add)
- (void)setIconToTopWithSpace:(CGFloat)spacing {
    CGSize imageSize = self.imageView.image.size;
    self.titleEdgeInsets = UIEdgeInsetsMake(
      0.0, - imageSize.width, - (imageSize.height + spacing), 0.0);

    CGSize titleSize = [self.titleLabel.text sizeWithAttributes:@{NSFontAttributeName: self.titleLabel.font}];
    self.imageEdgeInsets = UIEdgeInsetsMake(
      - (titleSize.height + spacing), 0.0, 0.0, - titleSize.width);

    CGFloat edgeOffset = fabs(titleSize.height - imageSize.height) / 2.0;
    self.contentEdgeInsets = UIEdgeInsetsMake(edgeOffset, 0.0, edgeOffset, 0.0);
}

- (void)setIconToRightWithSpace:(CGFloat)spacing {
    CGSize imageSize = self.imageView.image.size;
    self.titleEdgeInsets = UIEdgeInsetsMake(
      0.0, -(imageSize.width + spacing), 0, imageSize.width);

    CGSize titleSize = [self.titleLabel.text sizeWithAttributes:@{NSFontAttributeName: self.titleLabel.font}];
    self.imageEdgeInsets = UIEdgeInsetsMake(
      0, (titleSize.width + spacing), 0.0, -titleSize.width);

    CGFloat edgeOffset = fabs(titleSize.height - imageSize.height) / 2.0;
    self.contentEdgeInsets = UIEdgeInsetsMake(edgeOffset, 0.0, edgeOffset, 0.0);
}

- (void)setIconToLetfWithSpace:(CGFloat)spacing{
    self.titleEdgeInsets = UIEdgeInsetsMake(
      0.0, spacing, 0.0, 0.0);
    self.imageEdgeInsets = UIEdgeInsetsMake(
      0.0, - spacing, 0.0, 0.0);
}

- (void)setIconToBottomWithSpace:(CGFloat)spacing{
    CGSize imageSize = self.imageView.image.size;
    self.titleEdgeInsets = UIEdgeInsetsMake(
      0, -imageSize.width,  (imageSize.height + spacing), 0.0);

    CGSize titleSize = [self.titleLabel.text sizeWithAttributes:@{NSFontAttributeName: self.titleLabel.font}];
    self.imageEdgeInsets = UIEdgeInsetsMake(
    (titleSize.height + spacing), 0.0, 0.0, - titleSize.width);
    
    CGFloat edgeOffset = fabs(titleSize.height - imageSize.height) / 2.0;
    self.contentEdgeInsets = UIEdgeInsetsMake(edgeOffset, 0.0, edgeOffset, 0.0);
}


@end
